package com.myh.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.myh.springcloud.pojo.User;
import com.myh.springcloud.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/*
**2020年7月4日--下午12:17:13
**@version:
**莫耀华:
**@Description:
*/
@RestController
public class UserController {
	
	@Autowired
	private UserService userService;
	
	//失败调用的方法
	@HystrixCommand(fallbackMethod = "hystrixGet" )
	@GetMapping("/user/get/{id}")
	public User get(@PathVariable("id") Long id) {
		User user = userService.getUserById(id);
		if(user==null) {
			//如果为null1 抛出异常
			throw new RuntimeException("用户不存在");
		}
		return  user;
	}
	
	//备选方法
	public User hystrixGet(@PathVariable("id") Long id) {
		return new User(1L, "1", "1", "1", "1");
	}
}
